<?php
namespace Admins\Controller;
use Common\Controller\BaseController;
class PHPExcelController extends BaseController {


	public function _initialize()
	{
		set_time_limit(900);
		ini_set("memory_limit", "1024M");
		vendor('PHPExcel.PHPExcel','','.php');
	}
	public function index()
	{
/** PHPExcel_IOFactory */
//include 'PHPExcel/IOFactory.php';
vendor('PHPExcel.PHPExcel.IOFactory','','.php');


$inputFileName = 'D:/wamp/vhosts/ali.8g.com/Upload/111.xls';


//import('Common.Util.chunkReadFilter');


$inputFileType = 'Excel5';




$objPHPExcel = \PHPExcel_IOFactory::load($inputFileName); 


$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
var_dump($sheetData);


$sheet_count = $objPHPExcel->getSheetCount(); 

for ($s = 0; $s <$sheet_count; $s++) 
{ 
    $currentSheet = $objPHPExcel->getSheet($s);// 当前页 
    $row_num = $currentSheet->getHighestRow();// 当前页行数 
	$row_num = 10;
    $col_max = $currentSheet->getHighestColumn(); // 当前页最大列号 
 
//    // 循环从第二行开始，第一行往往是表头 
//    for($i = 2; $i <= $row_num; $i++) 
//    { 
//        $cell_values = array(); 
//        for($j = 'A'; $j <$col_max; $j++) 
//        { 
//            $address = $j . $i; // 单元格坐标 
//            $cell_values[] = $currentSheet->getCell($address)->getFormattedValue(); 
//        } 
// 
//         //看看数据 
//        print_r($cell_values); 
//    } 
} 


		
	}
	public function getdata($xlsFile,$groupId)
	{
	
		$inputFileName = './Uploads/'.$xlsFile;
		
		
		$PHPExcel = new \Admins\Model\PHPExcelModel();
		$res = $PHPExcel->getExcelAllData($inputFileName);
		$conf = $res[1];
		//var_dump($conf );
		
		//var_dump($res);
		//获取读取的规则--配置有关系
		
		$fields = c("XLSFIELD");
		foreach ($fields as $k=>$v)
		{
			$ruleField[$k] = $v;
		}
//		$ruleField['goodsID'] = c("XLSFIELD.goodsId");
//		$ruleField['taokeShortUrl'] = c("XLSFIELD.taokeShortUrl");
//		$ruleField['quanUrl'] = c("XLSFIELD.quanUrl");
//		$ruleField['taokeUrl'] = c("XLSFIELD.taokeUrl");
//		$ruleField['taoKouling'] = c("XLSFIELD.taoKouling");
//		$ruleField['quanKouling'] = c("XLSFIELD.quanKouling");
		
		
		$rowKey = getArrKey($conf,$ruleField);
		//var_dump($rowKey);
			
		unset($res[1]); //删除表头
		
		//定义新的录入数据格式
		$data = '';
		foreach($res as $k=>$v)
		{
			$product['groupId'] =  get_basename($xlsFile);;
			$product['goodsId'] = $v[$rowKey['goodsId']];
			$product['taokeShortUrl'] = $v[$rowKey['taokeShortUrl']];
			$product['quanUrl'] = $v[$rowKey['quanUrl']];
			$product['taokeUrl'] = $v[$rowKey['taokeUrl']];
			$product['taoKouling'] = $v[$rowKey['taoKouling']];
			$product['quanKouling'] = $v[$rowKey['quanKouling']];
			$product['time'] = date('Y-m-d H:i:s');

			
			$data[$k] = $product;
		}
		//var_dump($data);
		
		
//------------------------------
		$sql = '';	
		foreach($data as $v)
		{

			$isNewdata = $PHPExcel->onlyCheck($v['goodsId'],$groupId);
			
			if($isNewdata)
			{//更新操作语句
				$v['id'] = $isNewdata['id'];
				$sql .= $PHPExcel->getUpdateSql($v).';';
			}
			else
			{//插入操作语句
				
				$sql .= $PHPExcel->getInsertSql($v).';';
				
				
			}		
		}
		//echo $sql;
		//定义一个数组，把这些sql语句以一个引用（指针）传过去，不然直接把字符串当参数太大，太消耗内存了。
		$arraySql[] = $sql;
		//echo $arraySql;
		$result = $PHPExcel->moreExecute($arraySql);
		var_dump($result);	
		
	}

	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

}


?>